home *** CD-ROM | disk | FTP | other *** search
- //***************************************************************************
- // OATH :: Object-oriented Abstract Type Hierarchy
- //***************************************************************************
- //
- // Copyright (C) 1991, 1990 Texas Instruments Incorporated
- // Permission is granted to any individual or institution
- // to use, copy, modify, and distribute this software,
- // provided that this complete copyright and permission notice
- // is maintained, intact, in all copies and supporting documentation.
- //
- // Texas Instruments Incorporated provides this software "as is"
- // without express or implied warranty.
- //
- //***************************************************************************
- // pdlQueue (pdlQueueA, pdlQueueG)
- // pdlPos (pdlPosA, pdlPosG)
- //
- // History:
- // 07/91 Brian M Kennedy import, export, typeRegister
- // 06/91 Brian M Kennedy New macros & format; remove printDiagnostic
- // 10/90 Brian M Kennedy Major Rewrite
- // 02/90 Brian M Kennedy Original
- //
- //***************************************************************************
-
- #include "copyright.h"
-
- #include <oath/pdlQueue.h>
-
- #include <iostream.h>
-
- /////////////////////////////////////////////////////////////////////////////
- // pdlQueue Outlines
-
- OUTLINES(pdlQueue, lifoQueue)
-
- // Constructors //////////
-
- pdlQueueG::
- pdlQueueG (int IsConst) // IsConst = FALSE)
- :lifoQueueG(IsConst), Header(0), PosList(0)
- {ref();
- {Header = new slNodeP;}
- deref();
- }
-
- pdlQueueG::
- pdlQueueG (const seqG* S, int IsConst) // IsConst = FALSE)
- :lifoQueueG(IsConst), Header(0), PosList(0)
- {ref();
- {slNodeP* End = Header = new slNodeP;
- for(posA P = S->makePos(0, 0); P(); ++P)
- End = End->insertAfter((*P).guts());
- }
- deref();
- }
-
- pdlQueueG::
- pdlQueueG (const posG* Start, const posG* Beyond, int IsConst) // IsConst=FALSE
- :lifoQueueG(IsConst), Header(0), PosList(0)
- {ref();
- {slNodeP* End = Header = new slNodeP;
- if(!Beyond)
- for(posA P = (posA&)Start->makeCopy(0); P(); ++P)
- End = End->insertAfter((*P).guts());
- else
- {ensure(Start->parent() == Beyond->parent(),
- "The two pos's are not from the same seq!");
- slNodeP* End = Header;
- for(posA P = (posA&)Start->makeCopy(0);
- (Beyond != P.guts()) && P(); ++P)
- End = End->insertAfter((*P).guts());
- ensure(Beyond == P.guts(),"The second pos is not after the first!");
- }
- }
- deref();
- }
-
- pdlQueueG::
- ~pdlQueueG ()
- {assumed(!PosList, "pdlQueue deleted when it still has Pos's!");
- ref();
- {while(Header->next())
- Header->deleteNext();
- delete Header;
- }
- deref();
- }
-
-
- // oathCore Operations //////////
-
- void pdlQueueG::
- export (exportP& X) const
- {X.writeType(TypeName);
- int Count = count();
- X.stream() << Count << (isConst() ? ' ' : '\0');
- if(Count)
- {for(posA P = makePos(0, 0); P(); ++P)
- (*P).export(X);
- }
- }
-
- objA pdlQueueG::
- import (importP& M)
- {int Count;
- M.stream() >> Count;
- char MakeConst = M.stream().get();
- if(Count)
- {slNodeP * Header = new slNodeP;
- slNodeP * End = Header;
- for(int I = 0; I < Count; ++I)
- {objA Tmp = objA::import(M);
- End->Next = new slNodeP (0, Tmp.guts());
- End = End->Next;
- }
- return new pdlQueueG (Header, MakeConst);
- }
- else
- return new pdlQueueG (MakeConst);
- }
-
- void pdlQueueG::
- clearReferences()
- {clearMark();
- for(slNodeP *N = Header->next(); N; N = N->next())
- N->thisObj().guts()->deref();
- }
-
- void pdlQueueG::
- setReferences()
- {if(!isMarked())
- {setMark();
- for(slNodeP *N = Header->next(); N; N = N->next())
- {N->thisObj().guts()->ref();
- N->thisObj().guts()->setReferences();
- }
- }
- }
-
-
- // obj Operations //////////
-
- int pdlQueueG::
- isEqual (const objG* O) const
- {if(is(O))
- return TRUE;
- else if(!O->isImplementedAs(Type))
- return FALSE;
- else
- {const pdlQueueG* L = (const pdlQueueG*)O;
- posA Pthis = makePos(0, 0);
- posA PL = L->makePos(0, 0);
- while(TRUE)
- {if(Pthis.isPastEnd())
- return (PL.isPastEnd() ? TRUE : FALSE);
- if(PL.isPastEnd())
- return FALSE;
- if(!(*Pthis).is(*PL))
- return FALSE;
- ++Pthis;
- ++PL;
- }
- }
- }
-
- objA pdlQueueG::
- makeCopy (int MakeConst) const
- {slNodeP * NewHead = new slNodeP;
- slNodeP * NewEnd = NewHead;
- for(slNodeP * P = Header; P->next(); P = P->next())
- NewEnd = NewEnd->insertAfter(P->nextObj().guts());
- return new pdlQueueG (NewHead, MakeConst);
- }
-
- // bag Operations //////////
-
- int pdlQueueG::
- count () const
- {int C = 0;
- for(slNodeP *N = Header->next(); N; N = N->next())
- C++;
- return C;
- }
-
- int pdlQueueG::
- contains (const objG* O) const
- {for(slNodeP *N = Header->next(); N; N = N->next())
- if(O->is(N->thisObj().guts()))
- return TRUE;
- return FALSE;
- }
-
- int pdlQueueG::
- containsEqual (const objG* O) const
- {for(slNodeP *N = Header->next(); N; N = N->next())
- if(O->isEqual(N->thisObj().guts()))
- return TRUE;
- return FALSE;
- }
-
- int pdlQueueG::
- canContain (const objG*) const
- {return TRUE;}
-
- void pdlQueueG::
- insert (const objG* O)
- {NOT_CONST();
- Header->insertAfter(O);
- }
-
- void pdlQueueG::
- append (const bagG* B)
- {NOT_CONST();
- B->applyX(callSelf, this);
- }
-
- void pdlQueueG::
- apply (void (*F)(objA)) const
- {for(slNodeP *N = Header->next(); N; N = N->next())
- F(N->thisObj());
- }
-
- bagG* pdlQueueG::
- applyX (objA (*F)(objA), bagG* B) const
- {for(slNodeP *N = Header->next(); N; N = N->next())
- {objA O = F(N->thisObj());
- B->insert(O.guts());
- }
- return B;
- }
-
- bagG* pdlQueueG::
- applyX (bagA (*F)(objA), bagG* B) const
- {for(slNodeP *N = Header->next(); N; N = N->next())
- {bagA O = F(N->thisObj());
- B->append(O.guts());
- }
- return B;
- }
-
- bagA pdlQueueG::
- makeEmpty () const
- {return new pdlQueueG ();}
-
-
- // queue Operations //////////
-
- const objG* pdlQueueG::
- remove ()
- {NOT_CONST();
- assumed(!isEmpty(), "remove() attempted on an empty Queue!");
- adjustPosList(Header->next(), Header);
- const objG* O = Header->nextObj().guts();
- Header->deleteNext();
- return O;
- }
-
-
- // seq Operations //////////
-
- seqA pdlQueueG::
- makeSeq (const posG* Start, const posG* Beyond, int MakeConst) const
- {return new pdlQueueG (Start, Beyond, MakeConst);}
-
- seqA pdlQueueG::
- makeSeq (int Start, int Beyond, int MakeConst) const
- {posA S = makePos(Start, TRUE);
- posA B = makePos(Beyond,TRUE);
- return new pdlQueueG (S.guts(), B.guts(), MakeConst);
- }
-
-
- // lifoQueue Operations //////////
-
-
- // pdlQueue Operations //////////
-
- slNodeP* pdlQueueG::
- internalPosition (int I) const
- {if(!I)
- return Header;
- slNodeP * N = Header;
- for(int J = 0; (J < I) && N->next(); J++)
- N = N->next();
- return N;
- }
-
- void pdlQueueG::
- adjustPosList(slNodeP* CurrentPrev, slNodeP* NewPrev)
- {pdlPosG* Pos = PosList;
- while(Pos)
- {if(Pos->Prev == CurrentPrev)
- Pos->Prev = NewPrev;
- Pos = Pos->NextPos;
- }
- }
-
-
- /////////////////////////////////////////////////////////////////////////////
- // pdlPos Outlines
-
- OUTLINES(pdlPos, pos)
-
- pdlPosG::
- pdlPosG (const pdlQueueG* iList, slNodeP* iPrev, int IsConst) // IsConst = FALSE
- :posG(IsConst), List(iList), Prev(iPrev), PrevPos(0),
- NextPos(iList->posList())
- {if(NextPos)
- NextPos->PrevPos = this;
- List.guts()->posList() = this;
- }
-
- pdlPosG::
- ~pdlPosG ()
- {if(NextPos)
- NextPos->PrevPos = PrevPos;
- if(PrevPos)
- PrevPos->NextPos = NextPos;
- else
- List.guts()->posList() = NextPos;
- }
-
-
- // oathCore Operations //////////
-
- void pdlPosG::
- export (exportP& X) const
- {X.writeType(TypeName);
- List.export(X);
- int I = 0;
- pdlPosA P = List.makePos();
- while(P.guts()->Prev != Prev)
- {++P; ++I;}
- X.stream() << I << (isConst() ? ' ' : '\0');
- }
-
- objA pdlPosG::
- import (importP& M)
- {pdlQueueA Q = pdlQueueA::isa(objA::import(M));
- int I;
- M.stream() >> I;
- char MakeConst = M.stream().get();
- return Q.makePos(I, MakeConst);
- }
-
- void pdlPosG::
- clearReferences()
- {clearMark();
- List.guts()->deref();
- }
-
- void pdlPosG::
- setReferences()
- {if(!isMarked())
- {setMark();
- List.guts()->ref();
- List.guts()->setReferences();
- }
- }
-
-
- // obj Operations //////////
-
- int pdlPosG::
- isEqual (const objG* O) const
- {return O->isImplementedAs(Type) && (Prev == ((const pdlPosG*)O)->Prev);}
-
-
- // pos Operations //////////
-
- const objG* pdlPosG::
- indirection () const
- {assumed(Prev->Next, "operator * attempted past end of Sequence");
- return Prev->nextObj().guts();
- }
-
- void pdlPosG::
- increment ()
- {NOT_CONST();
- if(Prev->next())
- Prev = Prev->next();
- }
-
- void pdlPosG::
- increment (int I)
- {NOT_CONST();
- for(int J = 0; (J < I) && Prev->next(); J++)
- Prev = Prev->next();
- }
-
- const objG* pdlPosG::
- find (const objG* O)
- {NOT_CONST();
- slNodeP * P = Prev;
- slNodeP * N;
- while(N = P->next())
- {if(O->is(N->thisObj().guts()))
- {Prev = P;
- return O;
- }
- P = N;
- }
- return objG::Nil;
- }
-
- const objG* pdlPosG::
- findEqual (const objG* O)
- {NOT_CONST();
- slNodeP * P = Prev;
- slNodeP * N;
- while(N = P->next())
- {if(O->isEqual(N->thisObj().guts()))
- {Prev = P;
- return O;
- }
- P = N;
- }
- return objG::Nil;
- }
-
- void pdlPosG::
- reset (const posG* P)
- {NOT_CONST();
- ensure(P->isImplementedAs(Type) && List.is(((pdlPosG*)P)->List),
- "Pos's not from same seq!");
- Prev = ((const pdlPosG*)P)->Prev;
- }
-
- void pdlPosG::
- reset (int Index)
- {NOT_CONST();
- Prev = List.guts()->internalPosition(Index);
- }
-
-
- //***************************************************************************
-